package com.example.qfsx.bean;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.net.CookieHandler;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.qfsx.R;
import com.squareup.picasso.Picasso;

//用于将上下文、listview 子项布局的 id 和数据都传递过来
public class OrderAdapter extends ArrayAdapter<OrderItem> {
    public OrderAdapter(@NonNull Context context, int resource, @NonNull List<OrderItem> objects) {
        super(context, resource, objects);
    }
    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        OrderItem orderItem =getItem(position);//得到当前项的 Fruit 实例
        //为每一个子项加载设定的布局
        View view=LayoutInflater.from(getContext()).inflate(R.layout.order_item,parent,false);
        //分别获取 image view 和 textview 的实例
        ImageView goods_img2 = view.findViewById(R.id.goods_img2);
        TextView shop_price = view.findViewById(R.id.shop_price);
        TextView goods_name = view.findViewById(R.id.goods_name);
        TextView total_price = view.findViewById(R.id.total_price);
        // 设置要显示的图片和文字
        Picasso.get().load(orderItem.getGoodsImg2()).into(goods_img2);
        shop_price.setText("￥" + orderItem.getShopPrice());
        goods_name.setText(orderItem.getGoodsName());
        total_price.setText(Float.toString(orderItem.getShopPrice() * orderItem.getGoodsNumber()));
        return view;
    }
}